<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset=UTF-8>
    <title>Insert title here</title>
    <link rel="stylesheet" type="text/css"
          href="easyui/themes/bootstrap/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyui/demo/demo.css">
    <script type="text/javascript" src="myjs/path.js"></script>
    <script type="text/javascript" src="easyui/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<style type="text/css">
    .datagrid-footer {
        background-color: #f7c66c;
    }
</style>
<body>
<div id="tb">
    机构：<input id="jg" class="easyui-combotree" data-options=""
              style="width: 160px;">
    <script>
        $(function () {
            $.ajax({
                url: path + '/jg/tree.do',
                dataType: 'json',
                success: function (data) {
                    if (data.code != 400) {
                        $('#jg').combotree('loadData', data.rows);
                    } else {
                        $.messager.alert('提示', data.msg);
                    }
                }
            })
        })

        $('#jg').combotree({
            onClick: function (node) {
                load();
            }
        })
    </script>
    年份：<input class="easyui-combobox" id="year" style="width: 150px" ;
              data-options="valueField:'year',textField:'year',panelHeight:'auto'">
    季节：<input class="easyui-combobox" id="season" style="width: 150px" ;
              data-options="valueField:'season',textField:'season',panelHeight:'auto'">品牌：<input
        class="easyui-combobox" id="pinpai" style="width: 150px" ;
        data-options="valueField:'pinpai',textField:'pinpai',panelHeight:'auto'">
    <label><input name="chima" class="chima" type="radio"
                  value="2" onclick="load()"/>按尺码 </label> <label><input
        name="chima" class="chima" type="radio" value="1"
        onclick="load()"/>按颜色 </label> <label><input name="chima"
                                                     class="chima" type="radio" value="0" onclick="load()"
                                                     checked="checked"/>按款号 </label> <input id="product"
                                                                                            class="easyui-searchbox"
                                                                                            data-options="searcher:qq,prompt:'请输入款号关键词'"/>
    <a iconCls="icon-save" class="easyui-linkbutton" type="button"
       onclick="daochu()">导出Excel</a>
    <script type="text/javascript">
        function qq(value, name) {
            load();
        }
    </script>
</div>

<table class="easyui-datagrid" id="store">

</table>
<input id='jgid' type="hidden"
       value="<%=request.getParameter("jgid")%>">
</body>
</html>
<script>
    function daochu() {
        var fenlei = $('input:radio[name="chima"]:checked').val();
        var jgid = $("#jg").combotree("getValue");
        var year = $("#year").combobox("getValue");
        var season = $("#season").combobox("getValue");
        var pinpai = $("#pinpai").combobox("getValue");
        var product = $("#product").searchbox("getValue");
        window.location.href = path + "/storereport/downloadexcel.xls?fenlei=" + fenlei + "&jgid=" + jgid + "&year=" + year
            + "&season=" + season + "&pinpai=" + pinpai + "&product=" + product;
    }


    $.ajax({
        url: path + "/production/selectallattr.do",
        type: 'post',
        dataType: "json",
        success: function (data) {
            $('#season').combobox("loadData", data.data.seasonx);
            $('#year').combobox("loadData", data.data.yearx);
            $('#pinpai').combobox("loadData", data.data.pinpaix);
        }
    })


    $("#jg").combotree({
        onSelect: function (node) {
        },
        onLoadSuccess: function (node, data) {
            $("#jg").combotree('setValue', data[0].id);
            var chima = $('input:radio[name="chima"]:checked').val();
            var jgid = $("#jg").combotree("getValue");
            $("#store").datagrid({
                url: path + '/storereport/list.do',
                queryParams: {
                    product: $("#product").searchbox("getValue"),
                    fenlei: chima,
                    jgid: data[0].id,
                    year: $("#year").combobox("getValue"),
                    season: $("#season").combobox("getValue"),
                    pinpai: $("#pinpai").combobox("getValue")
                }
            })
        }
    });

    function load() {
        var chima = $('input:radio[name="chima"]:checked').val();
        var jgid = $("#jg").combotree("getValue");
        $("#store").datagrid({
            url: path + '/storereport/list.do',
            queryParams: {
                product: $("#product").searchbox("getValue"),
                fenlei: chima,
                jgid: jgid,
                year: $("#year").combobox("getValue"),
                season: $("#season").combobox("getValue"),
                pinpai: $("#pinpai").combobox("getValue")
            }
        })
    }
    //加载数据
    $('#store')
        .datagrid(
            {
                width: 'auto',
                toolbar: '#tb',
                //height : 340,
                striped: true,
                singleSelect: true,
                loadMsg: '数据加载中请稍后……',
                iconCls: 'icon-edit',
                pagination: true,
                pageSize: 10,//分页大小
                striped: false,
                pageList: [10, 20, 30, 40, 50],//每页的个数
                fit: true,//自动补全
                showFooter: true,
                remoteSort: true,
                sortable: true,
                sortName: 'shuliang',
                sortOrder: 'desc',
                columns: [[{
                    field: 'product',
                    title: '商品款号',
                    align: 'center',
                    width: 150
                }, {
                    field: 'productName',
                    title: '商品名称',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'price',
                    title: '铭牌价/￥',
                    align: 'right',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        if (value == null) {
                            return 0.00
                        }
                        return value.toFixed(2);
                    }
                }, {
                    field: 'color',
                    title: '颜色',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'size',
                    title: '尺码',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'caijianshu',
                    title: '总裁剪/件',
                    align: 'right',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'rukushu',
                    title: '已入库/件',
                    align: 'right',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'chukushu',
                    title: '已出库/件',
                    align: 'right',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'shuliang',
                    title: '现有库存/件',
                    align: 'right',
                    width: 100,
                    sortable: true
                }, {
                    field: 'zhongshuliang',
                    title: '小计',
                    align: 'right',
                    width: 100
                }, {
                    field: 'pinpai',
                    title: '品牌',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'season',
                    title: '季节',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }, {
                    field: 'year',
                    title: '年份',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (row.product == "总计：") {
                            return '-- --'
                        }
                        return value;
                    }
                }]],
                onLoadSuccess: function (data) {
                    if ($("#jg").combotree("getValue") != 1) {
                        $("#store").datagrid("hideColumn", "caijianshu"); // 设置隐藏列
                    }
                    if ($('input:radio[name="chima"]:checked').val() == 0) {
                        $("#store").datagrid("hideColumn", "size"); // 设置隐藏列
                        $("#store").datagrid("hideColumn", "zhongshuliang");
                        $("#store").datagrid("hideColumn", "color"); // 设置隐藏列
                    }
                    if ($('input:radio[name="chima"]:checked').val() == 1) {
                        $("#store").datagrid("hideColumn", "size"); // 设置隐藏列
                        $("#store").datagrid("showColumn", "color"); // 设置隐藏列
                        $("#store").datagrid("showColumn", "zhongshuliang");

                    }
                    if ($('input:radio[name="chima"]:checked').val() == 2) {
                        $("#store").datagrid("showColumn", "size"); // 设置隐藏列
                        $("#store").datagrid("showColumn", "zhongshuliang");
                        $("#store").datagrid("showColumn", "color"); // 设置隐藏列
                    }
                    //给表格增加一行解决最后小计不算的问题
                    $('#store').datagrid('appendRow', {
                        product: '',
                        price: 0.00
                    });

                    var color = 1;
                    var size = 1;
                    var zongji = 0;
                    var columns = $('#store').datagrid("options").columns;
                    var rows = $('#store').datagrid("getRows");
                    var count = 0;
                    for (var i = 1; i < data.rows.length; i++) {
                        if (zongji == 0) {
                            zongji += data.rows[i - 1]['shuliang']
                        }
                        if (data.rows[i]['product'] == data.rows[i - 1]['product']) {
                            count++;
                            zongji += data.rows[i]['shuliang']
                        } else {
                            rows[i - 1 - count][columns[0][9].field] = zongji;
                            $('#store').datagrid('refreshRow', i - 1 - count);
                            zongji = 0;
                            count = 0;
                        }
                    }
                    //删除最后一行
                    $('#store').datagrid('deleteRow', data.rows.length - 1);

                    for (var i = 1; i < data.rows.length; i++) {
                        if (data.rows[i]['product'] == data.rows[i - 1]['product']) {
                            color += 1;
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - color,
                                field: 'product',  //需要合并的列
                                rowspan: color     //纵向合并的格数，如果想要横向合并，就使用colspan：color
                            });
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - color,
                                field: 'price',  //需要合并的列
                                rowspan: color     //纵向合并的格数，如果想要横向合并，就使用colspan：color
                            });
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - color,
                                field: 'productName',  //需要合并的列
                                rowspan: color     //纵向合并的格数，如果想要横向合并，就使用colspan：color
                            });
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - color,
                                field: 'zongshuliang',
                                rowspan: color
                            });
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - color,
                                field: 'pinpai',
                                rowspan: color
                            });
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - color,
                                field: 'zhongshuliang',
                                rowspan: color
                            });
                        } else {
                            color = 1;     //一旦前后两行的值不一样了，那么需要合并的格子数color就需要重新计算
                        }
                        if (data.rows[i]['color'] == data.rows[i - 1]['color'] && color != 1) {
                            size += 1;
                            $(this).datagrid('mergeCells', {
                                index: i + 1 - size,
                                field: 'color',
                                rowspan: size
                            });
                        } else {
                            size = 1;     //一旦前后两行的值不一样了，那么需要合并的格子数color就需要重新计算
                        }
                    }
                    $("#product").next().find("input").eq(0).focus();
                },
                onClickRow: function (rowIndex, rowData) {
                    $(this).datagrid('unselectRow', rowIndex);
                }
            })
</script>

